import { ReadlineParser, SerialPort } from 'serialport';
const parser = new ReadlineParser();

export const createSerialPort = async () => {
  try {
    const port = new SerialPort(
      {
        path: 'COM4',
        baudRate: 115200,
      },
      (err) => {
        if (err) {
          console.log(err);
        }
      }
    );
    port.pipe(parser);
    port.write('ROBOT PLEASE RESPOND\n');
    await scan();
    return port;
  } catch (error) {}
};
export const scan = async () => {
  const ports = await SerialPort.list();
  return ports;
};
parser.on('data', (data) => {
  console.log('收到数据', data);
});
export { parser };
